﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Kiiro.ExceptionManagement;
using Kiiro.Common.Utility;

namespace Kiiro.Model.HttpModule
{
    class KiiroHttpModule : IHttpModule
    {
        public void Dispose ( )
        {
        }

        public void Init ( HttpApplication context )
        {
            context.EndRequest += new EventHandler ( context_KiiroAccessDenied );
            context.Error += new EventHandler ( context_KiiroError );
        }

        void context_KiiroAccessDenied ( object sender, EventArgs e )
        {
            try
            {
                HttpApplication httpApplication = sender as HttpApplication;
                HttpContext context = httpApplication.Context;   
                HttpRequest request = context.Request;

                string incomingUrl = request.Url.ToString ( );
                if ( incomingUrl.ToLower ( ).Contains ( "/_layouts/accessdenied.aspx" ) &&
                    !incomingUrl.ToLower ( ).Contains ( "loginasanotheruser=true" ) )
                {
                    Guid siteCollectionId = Guid.Empty;
                    string serverRelativeUrl = null;

                    string source = request.Params [ "Source" ];
                    string absUrl = source.Contains ( "://" ) ? source : request.Url.Scheme + "://" + request.Url.Authority + source;
                    SPHelpers.GetSiteCollectionIdAndServerRelativeUrl ( absUrl, out siteCollectionId, out serverRelativeUrl );

                    using ( SPWeb web = SPHelpers.GetSystemWeb ( siteCollectionId, serverRelativeUrl ) )
                    {
                        if ( SPHelpers.IsKiiro ( web ) )
                        {
                            serverRelativeUrl = web.ServerRelativeUrl;

                            if (SPHelpers.IsProject(web))
                                serverRelativeUrl =web.ServerRelativeUrl.Substring ( 0, web.ServerRelativeUrl.LastIndexOf ( "/" ));

                            string kiiroAccessDeniedUrl = string.Format ( "{0}://{1}{2}/AccessDenied.aspx",
                                request.Url.Scheme, request.Url.Authority, serverRelativeUrl );

                            HttpContext.Current.Server.ClearError ( );
                            HttpContext.Current.Response.Clear ( );

                            HttpContext.Current.Response.Redirect ( kiiroAccessDeniedUrl, false );
                        }
                    }
                }
            }
            catch ( Exception ex )
            {
                ExceptionManager.HandleException ( ex, true );
            }
        }

        void context_KiiroError ( object sender, EventArgs e )
        {
            try
            {
                if ( SPHelpers.IsKiiro ( SPContext.Current.Web ) )
                {
                    Exception [ ] unhandledExceptions = HttpContext.Current.AllErrors;
                    foreach ( Exception ex in unhandledExceptions )
                        ExceptionManager.HandleException ( ex, false );

                    HttpContext.Current.Server.ClearError ( );
                    HttpContext.Current.Response.Clear ( );

                    HttpContext.Current.Server.Transfer ( "~/KiiroError.aspx", false );
                }
            }
            catch ( Exception ex )
            {
                ExceptionManager.HandleException ( ex, true );
            }
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.Model/HttpModule/KiiroHttpModule.cs $
 * 
 * 9     18/11/09 12:24p Camerons
 * 
 * 8     6/11/09 8:55p Camerons
 * 
 * 7     2/11/09 7:16p Camerons
 * 
 * 6     28/10/09 6:09p Camerons
 * 
 * 5     28/10/09 1:41p Camerons
 * 
 * 4     28/10/09 12:16p Camerons
 * 
 * 3     28/10/09 12:03p Camerons
 * 
 * 2     10/28/09 10:26a Adamw
 * added check for :// to allow for a valid siteCollectionId (Guid.Empty
 * otherwise) and relativeUrl to be returned in AccessDenied method
 * 
 * 1     26/10/09 6:35p Camerons
 * 
 ************************************************************************/
